package GUI.Ventas;

import java.rmi.RemoteException;
import java.util.ArrayList;

import org.eclipse.swt.widgets.Dialog;
import org.eclipse.swt.widgets.Display;
import org.eclipse.swt.widgets.Shell;
import org.eclipse.swt.widgets.Table;
import org.eclipse.swt.widgets.TableItem;
import org.eclipse.swt.SWT;
import org.eclipse.swt.widgets.TableColumn;

import sale.entities.OrderVO;
import sale.entities.SaleVO;
import sale.exceptions.EmptySaleExcpetion;
import sale.exceptions.NoSalesException;
import service.ServiceFacade;
import service.article.exceptions.ProxyException;
import service.sale.interfaces.SaleProxyMgt;

import org.eclipse.swt.widgets.Button;
import org.eclipse.swt.events.SelectionAdapter;
import org.eclipse.swt.events.SelectionEvent;

import GUI.Administracion.Exceptions.ExcepcionesMenuVentas;
import GUI.Ventas.Facturacion.CerrarVenta;

public class VetnasAbiertasDelivery extends Dialog {

	protected Object result;
	protected Shell shell;
	private Table table;
	int opcion;
	/**
	 * Create the dialog.
	 * @param parent
	 * @param style
	 */
	public VetnasAbiertasDelivery(Shell parent, int style, int op) {
		super(parent, style);
		setText("SWT Dialog");
		opcion = op;
	}

	/**
	 * Open the dialog.
	 * @return the result
	 * @throws NoSalesException 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	public Object open() throws RemoteException, ProxyException, NoSalesException {
		createContents();
		shell.open();
		shell.layout();
		Display display = getParent().getDisplay();
		while (!shell.isDisposed()) {
			if (!display.readAndDispatch()) {
				display.sleep();
			}
		}
		return result;
	}

	/**
	 * Create contents of the dialog.
	 * @throws NoSalesException 
	 * @throws ProxyException 
	 * @throws RemoteException 
	 */
	private void createContents() throws RemoteException, ProxyException, NoSalesException {
		shell = new Shell(getParent(), getStyle());
		shell.setSize(723, 371);
		shell.setText(getText());
		
		table = new Table(shell, SWT.BORDER | SWT.FULL_SELECTION);
		table.setBounds(30, 31, 683, 237);
		table.setHeaderVisible(true);
		table.setLinesVisible(true);
		
		TableColumn tblclmnCodigoDeVenta = new TableColumn(table, SWT.NONE);
		tblclmnCodigoDeVenta.setWidth(100);
		tblclmnCodigoDeVenta.setText("Codigo de Venta");
		
		TableColumn tblclmnNewColumn = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn.setWidth(169);
		tblclmnNewColumn.setText("Direccion y Telefono");
		
		TableColumn tblclmnNewColumn_1 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_1.setWidth(231);
		tblclmnNewColumn_1.setText("Articulos");
		
		TableColumn tblclmnNewColumn_2 = new TableColumn(table, SWT.NONE);
		tblclmnNewColumn_2.setWidth(190);
		tblclmnNewColumn_2.setText("Especificaciones");
		
		Button btnEditarOrden = new Button(shell, SWT.NONE);
		
		btnEditarOrden.setBounds(325, 296, 94, 28);
		
		SaleProxyMgt aux = ServiceFacade.getInstance().getSaleProxyMgt();
		final SaleVO[] sales = aux.getOpenDeliverySales();
		if (opcion ==0){
			btnEditarOrden.setVisible(false);
		}
		if(opcion==1){
			btnEditarOrden.setText("Editar Orden");
		}
		if(opcion==2){
			btnEditarOrden.setText("Cerrar Orden");
		}
		System.out.println("numero de ventas " + sales.length);
		for(int i=0;i<sales.length;i++){
			String m= "";
			String e="";
			ArrayList <OrderVO> ordenes =sales[i].getOrders();
			for (int k=0;k<ordenes.size();k++){
				
				for(int j=0;j<ordenes.get(k).getArts().size();j++){
					m=m+ " " + ordenes.get(k).getArts().get(j).getName();
					if(ordenes.get(k).getEspecif()!=null){
						e=e+ " " + ordenes.get(k).getEspecif().get(j);
						}
				}}
					TableItem tableItem = new TableItem(table, SWT.NONE);
					tableItem.setText(2,m);
					tableItem.setText(1, sales[i].getTel() + " " +  sales[i].getAddress());
					
					if(e.equals("")){
						tableItem.setText(3, "no hay especificaciones");
					}
					else{
						tableItem.setText(3,e);
					tableItem.setText(0, String.valueOf(sales[i].getSaleKey()));
				
			
		
	}
		}
	
	btnEditarOrden.addSelectionListener(new SelectionAdapter() {
		@Override
		public void widgetSelected(SelectionEvent e) {
			if(opcion==1){
				SaleProxyMgt aux = ServiceFacade.getInstance().getSaleProxyMgt();
				SaleVO[] s = null;
				try {
					s = aux.getOpenDeliverySales();
				} catch (RemoteException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				} catch (ProxyException e2) {
					ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
					ventana.open();
					shell.dispose();
					e2.printStackTrace();
				} catch (NoSalesException e2) {
					// TODO Auto-generated catch block
					e2.printStackTrace();
				}
			TableItem[] items = table.getSelection();
			SaleVO sale = null;
			System.out.println ("toy");
			
			for (int i=0; i<s.length;i++){
				if(items[0].getText(0).equalsIgnoreCase(String.valueOf(s[i].getSaleKey()))){
					sale = s[i];
					System.out.println ("tengo la venta");
				}
			}
			//Shell parent, int style, SaleVO sale, int m, int op, OrderVO o
			AgregarArticulosDelivery a = new AgregarArticulosDelivery (shell, shell.getStyle(), sale, -1, 6, null);
			shell.setVisible(false);
			try {
				System.out.println("otra ventana");
				a.open();
			} catch (RemoteException e1) {
				ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
				ventana.open();
				shell.dispose();
				e1.printStackTrace();
			} catch (ProxyException e1) {
				ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
				ventana.open();
				shell.dispose();
				e1.printStackTrace();
			}
		}
		if(opcion==2){
		TableItem[] items = table.getSelection();
		SaleVO sale = null;
		for (int i=0; i<sales.length;i++){
			if(items[0].getText(0).equalsIgnoreCase(String.valueOf(sales[i].getSaleKey()))){
				sale = sales[i];
			}
		}
		CerrarVenta cerrar = new CerrarVenta (shell, shell.getStyle(), sale, null);
		shell.setVisible(false);
		try {
			cerrar.open();
		} catch (RemoteException e1) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e1.printStackTrace();
		} catch (ProxyException e1) {
			ExcepcionesMenuVentas ventana = new ExcepcionesMenuVentas (shell, shell.getStyle(), "Error de Conexion con el Servidor");
			ventana.open();
			shell.dispose();
			e1.printStackTrace();
		} catch (EmptySaleExcpetion e1) {
			// TODO Auto-generated catch block
			e1.printStackTrace();
		}
			
		}}
	});
}}
